<script setup>
import { useSellerStore } from '@/stores/sellerStore'
import { ref, reactive } from 'vue'
import { useUpdateSellerAPI,useUpdateAvatarAPI } from '@/apis/seller'

const uploadRef = ref()

// 定义响应式状态
const activeTab = ref('member') // 默认显示会员中心

const sellerStore = useSellerStore()
const userInfo = ref({})
userInfo.value = sellerStore.sellerInfo.data.user


//弹窗控制
const dialog = ref(false)
const loading = ref(false)

//下面是用户信息的方法
const userForm = reactive({
    id: userInfo.value.id,
    password: userInfo.value.password,
    nickname: userInfo.value.nickname,
    gender: userInfo.value.gender,
    phone: userInfo.value.phone,
    email: userInfo.value.email
})

const userSubmit = async () => {
    loading.value = true
    const res = await useUpdateSellerAPI(userForm)
    if (res.data.code === 0) loading.value = false
    setTimeout(() => {
        dialog.value = false
    }, 400)
    // 重新获取用户数据
    sellerStore.getNewSellerInfo()
}

const handleClose = (done) => {
    if (loading.value) {
        return
    }
    ElMessageBox.confirm('你想要提交数据吗?', '误触了吗？', {
        confirmButtonText: '确认',
        cancelButtonText: '取消'
    })
        .then(async () => {
            loading.value = true
            const res = await useUpdateUserAPI(userForm)
            if (res.data.code === 0) loading.value = false
            sellerStore.getNewSellerInfo()
            timer = setTimeout(() => {
                done()
                // 动画关闭需要一定的时间
                setTimeout(() => {
                    loading.value = false
                }, 400)
            }, 2000)
        })
        .catch(() => {
            // catch error
        })

}

const cancelForm = () => {
    loading.value = false
    dialog.value = false
    clearTimeout(timer)
}

//用户头像地址
const imgUrl = ref(sellerStore.sellerInfo.data.user.userPic)

//下面是上传图片阿里云成功的回调函数
const uploadSuccess = (result) =>{
    imgUrl.value = result.data;
}

//修改用户头像开始
const updateAvatar = async () => {
    let res = await useUpdateAvatarAPI(imgUrl.value)
    ElMessage.success(res.message?res.message:'修改成功')
    sellerStore.getNewSellerInfo()
}

</script>

<template>
    <div class="home-overview">
        <!-- 用户信息 -->
        <div class="user-meta">
            <div class="avatar">
                <img :src="sellerStore.sellerInfo.data.user.userPic" />
            </div>
            <h4>{{ sellerStore.sellerInfo.data.user.nickname }}</h4>
        </div>
        <!-- 导航菜单 -->
        <div class="item">
            <a href="javascript:;" @click="activeTab = 'member'">
                <span class="iconfont icon-huiyuanzhongxin"></span>
                <p>会员中心</p>
            </a>
            <a href="javascript:;" @click="activeTab = 'avatar'">
                <span class="iconfont icon-SUI_xiugaitouxiang"></span>
                <p>更改头像</p>
            </a>
        </div>
    </div>

    <!-- 内容区域 -->
    <div class="like-container">
        <!-- 会员中心 -->
        <div class="home-panel" v-show="activeTab === 'member'">
            <div class="header">
                <h4>个人资料</h4>
            </div>
            <div class="content">
                <!-- 会员中心内容 -->
                <el-form label-width="auto" style="max-width: 600px">
                    <el-form-item label="账户 :">
                        <el-input v-model="sellerStore.sellerInfo.data.user.username" disabled />
                    </el-form-item>
                    <el-form-item label="昵称 :">
                        <el-input v-model="sellerStore.sellerInfo.data.user.nickname" disabled />
                    </el-form-item>
                    <el-form-item label="性别 :">
                        <el-input v-model="sellerStore.sellerInfo.data.user.gender" disabled />
                    </el-form-item>
                    <el-form-item label="手机 :">
                        <el-input v-model="sellerStore.sellerInfo.data.user.phone" disabled />
                    </el-form-item>
                    <el-form-item label="邮箱 :">
                        <el-input v-model="sellerStore.sellerInfo.data.user.email" disabled />
                    </el-form-item>
                </el-form>
            </div>
            <div class="content">
                <el-button type="primary" @click="dialog = true">补充/修改</el-button>
            </div>
        </div>

        <!-- 更改头像 -->
        <div class="home-panel" v-show="activeTab === 'avatar'">
            <div class="header">
                <h4>更改头像</h4>
            </div>
            <!-- 头像上传组件 -->
            <el-upload 
                ref="uploadRef" 
                class="avatar-uploader" 
                :show-file-list="false"
                :auto-upload="true"
                action="/api/upload"
                name="file"
                :headers="{'Authorization':sellerStore.sellerInfo.data.token}"
                :on-success="uploadSuccess"
                >
                <img v-if="imgUrl" :src="imgUrl" class="avatar" />
                <img v-else :src="avatar" width="247" />
            </el-upload>
            <el-button type="primary" :icon="Plus" size="large" @click="uploadRef.$el.querySelector('input').click()">
                选择图片
            </el-button>
            <el-button type="success" :icon="Upload" size="large" @click="updateAvatar">
                上传头像
            </el-button>
        </div>
    </div>
    <!-- 用户弹出页是这个 -->
    <el-drawer v-model="dialog" title="修改用户信息" :before-close="handleClose" direction="rtl" size="50%">
        <div>
            <el-form :model="userForm">
                <el-form-item label="昵称" :label-width="formLabelWidth">
                    <el-input v-model="userForm.nickname" autocomplete="off" />
                </el-form-item>
                <el-form-item label="性别">
                    <el-select v-model="userForm.gender" placeholder="请选择你的性别" clearable>
                        <!-- 使用 :value 绑定不同类型 -->
                        <el-option label="保密" :value="2" />
                        <el-option label="男" :value="1" />
                        <el-option label="女" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item label="手机" :label-width="formLabelWidth">
                    <el-input v-model="userForm.phone" autocomplete="off" />
                </el-form-item>
                <el-form-item label="邮箱" :label-width="formLabelWidth">
                    <el-input v-model="userForm.email" autocomplete="off" />
                </el-form-item>
            </el-form>
            <div>
                <el-button @click="cancelForm">取消</el-button>
                <el-button type="primary" :loading="loading" @click="userSubmit">
                    {{ loading ? '提交中 ...' : '提交' }}
                </el-button>
            </div>
        </div>
    </el-drawer>
</template>

<style scoped lang="scss">
.home-overview {
    height: 132px;
    background: url(@/assets/images/center-bg.png) no-repeat center / cover;
    display: flex;

    .user-meta {
        flex: 1;
        display: flex;
        align-items: center;

        .avatar {
            width: 85px;
            height: 85px;
            border-radius: 50%;
            overflow: hidden;
            margin-left: 60px;

            img {
                width: 100%;
                height: 100%;
            }
        }

        h4 {
            padding-left: 26px;
            font-size: 18px;
            font-weight: normal;
            color: white;
        }
    }

    .item {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-around;

        &:first-child {
            border-right: 1px solid #f4f4f4;
        }

        a {
            color: white;
            font-size: 16px;
            text-align: center;

            .iconfont {
                font-size: 32px;
            }

            p {
                line-height: 32px;
            }
        }
    }
}

.like-container {
    margin-top: 20px;
    border-radius: 4px;
    background-color: #fff;
}

.home-panel {
    background-color: #fff;
    padding: 0 20px;
    margin-top: 20px;
    height: 400px;

    .header {
        height: 66px;
        border-bottom: 1px solid #f5f5f5;
        padding: 18px 0;
        display: flex;
        justify-content: space-between;
        align-items: baseline;

        h4 {
            font-size: 22px;
            font-weight: 400;
        }

    }

    .avatar-uploader {
        :deep() {
            .avatar {
                width: 247px;
                height: 247px;
                display: block;
            }

            .el-upload {
                border: 1px dashed var(--el-border-color);
                border-radius: 6px;
                cursor: pointer;
                position: relative;
                overflow: hidden;
                transition: var(--el-transition-duration-fast);
            }

            .el-upload:hover {
                border-color: var(--el-color-primary);
            }

            .el-icon.avatar-uploader-icon {
                font-size: 28px;
                color: #8c939d;
                width: 278px;
                height: 278px;
                text-align: center;
            }
        }
    }
}
</style>